package com.wjy.Data_Structure.queue;

import com.wjy.Data_Structure.linearlist.common.SLNode;

public class QueueSLinked implements Queue {
	private SLNode front; // 队首指针
	private SLNode rear; // 队尾指针
	private int size; // 元素个数

	public QueueSLinked() {
		this.front = new SLNode();
		this.rear = front;
		this.size = 0;
	}

	@Override
	public int getSize() {
		return size;
	}

	@Override
	public boolean isEmpty() {
		return size == 0;
	}

	@Override
	public void enqueue(Object e) {
		SLNode p = new SLNode(e, null);
		rear.setNext(p);
		rear = p;
		size++;

	}

	@Override
	public Object dequeue() throws QueueEmptyException {
		if (size < 1)
			throw new QueueEmptyException("错误：队列为空");
		SLNode p = front.getNext();
		front.setNext(p.getNext());
		size--;
		if (size < 1)
			rear = front; // 如果队列为空，rear指向头结点
		return p.getData();
	}

	@Override
	public Object peek() throws QueueEmptyException {
		if (size < 1)
			throw new QueueEmptyException("错误：队列为空");
		return front.getNext().getData();
	}

}
